package com.esyoga.yoyoga.handler;

import com.esyoga.yoyoga.pojo.DTO.EachOtherAttentionDTO;
import com.esyoga.yoyoga.service.AttentionService;
import com.esyoga.yoyoga.util.CutPagesUtil;
import com.esyoga.yoyoga.util.WebSocketUtil;
import lombok.Data;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/*
关注表
 */
@Data
@Controller
@RequestMapping("/attention")
public class AttentionHandler {
    @Resource
    AttentionService attentionService;
//当前用户关注另一个用户（当前用户id为userid1，类型为：usertype1，另一用户id为userid2，类型为：usertype2）（单向）
    @RequestMapping("/addOneAttention")
    @ResponseBody
    public String addOneAttention(Integer userid1 ,Integer usertype1, Integer userid2 ,Integer usertype2 ){
       String result= attentionService.addOneAttention(userid1,usertype1,userid2,usertype2);
        return result;
    }
    //取消关注(单向)
    @RequestMapping("/delOneAttention")
    @ResponseBody
    public String delOneAttention(Integer userid1 ,Integer usertype1, Integer userid2 ,Integer usertype2 ){
       String result= attentionService.delOneAttention(userid1,usertype1,userid2,usertype2);
        return result;
    }

    //判断当前用户是否已经关注了对方
    @RequestMapping("/isAttention")
    @ResponseBody
    public String isAttention(Integer userid1 ,Integer usertype1, Integer userid2 ,Integer usertype2 ){
       String result= attentionService.isAttention(userid1,usertype1,userid2,usertype2);
        return result;
    }
//查询与当前用户是互相关注的人，传入当前用户的id和类型(用于显示他的好友) 分页
@RequestMapping("/findEachOtherAttention")
@ResponseBody
    public Map<String,Object> findEachOtherAttention(Integer page,Integer userid1 , Integer usertype1){
    List<EachOtherAttentionDTO> eachOtherAttentionDTOList=attentionService.findEachOtherAttention(userid1,usertype1);
    CutPagesUtil.clear();
    CutPagesUtil.setPageInfo(page,10);
    List<EachOtherAttentionDTO> eachOtherAttentionDTOS=CutPagesUtil.getPages(eachOtherAttentionDTOList);
    int total= CutPagesUtil.getTotalPage();
    Map<String,Object> result=new HashMap<String,Object>();
    result.put("total",total);
    result.put("eachOtherAttentionDTOS",eachOtherAttentionDTOS);
    return result;
    }


    @RequestMapping("/findEachOtherAttentionall")
    @ResponseBody
    public Map<String,Object> findEachOtherAttentionall(Integer userid1 , Integer usertype1){
        List<EachOtherAttentionDTO> eachOtherAttentionDTOList=attentionService.findEachOtherAttention(userid1,usertype1);
       Set<String> online=WebSocketUtil.SESSIONMAP.keySet();
Map<String,Object> message=new HashMap<>();
message.put("eachOtherAttentionDTOList",eachOtherAttentionDTOList);
message.put("online",online);
        System.out.println(message);
        return message;
    }

}
